package com.clinic.action.user;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.entity.gui.GuiAppointment;
import com.clinic.entity.gui.GuiPagination;
import com.clinic.entity.gui.GuiPatient;
import com.clinic.entity.param.Index;
import com.clinic.entity.type.SortProperty;
import com.clinic.logic.appointment.AppointmentLogic;
import com.clinic.logic.user.PatientLogic;
import com.clinic.util.CommonUtil;

public class PatientAction extends UserAction {

	private static final long serialVersionUID = -4103984059329344242L;
	
	@Autowired
	private PatientLogic patientLogic;
	@Autowired
	private AppointmentLogic appointmentLogic;

	private int size;
	private String term;
	private SortProperty sort;
	private int page;
	private GuiPagination guiPagination;
	private GuiPatient guiPatient;
	private List<GuiPatient> guiPatientList;
	private GuiAppointment guiAppointment;
	private String todayDate;
	
	public String registration() {
		if(isDoctorLogin() || isAssistantLogin()) {
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String show(){
		if (isDoctorLogin() || isAssistantLogin() || (isPatientLogin() && id == getLoginUser().getUserId())) {
			guiPatient = patientLogic.getPatientDetailsById(id);
			guiAppointment = appointmentLogic.getUpcomingPatientAppointment(guiPatient.getUserId(), CommonUtil.getTodayDate());
			todayDate = CommonUtil.getDateStringFormatByDate(CommonUtil.getTodayDate());
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String edit(){
		if(isDoctorLogin() || isAssistantLogin()) {
			guiPatient = patientLogic.getPatientDetailsById(id);
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String delete(){
		if(isDoctorLogin() && getLoginUser().isAdmin() == true) {
			guiPatient = patientLogic.getPatientDetailsById(id);
			if(guiPatient.isAvailable()) {
				patientLogic.deletePatientDetailsById(id);
				return ActionResult.SUCCESS;
			}
			return ActionResult.ERROR;
		}	
		return ActionResult.AUTH_FAILED;
	}
	
	public String insert(){
		if(isDoctorLogin() || isAssistantLogin()) {
			boolean isValid = patientLogic.isValidPatientInsert(guiPatient);
			if(!isValid){
				addActionError("Failed to insert.Please fill all neccassary fields");
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiPatient = patientLogic.insertPatient(guiPatient);  // catch guiPatient object details returned from inserPatient()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}

	public String update(){
		if(isDoctorLogin() || isAssistantLogin()) {
			boolean isValidPatientUpdate = patientLogic.isValidPatientUpdate(guiPatient);
			if(!isValidPatientUpdate) {
				addActionError("Failed to insert.This docotr's is already registered");
			}
			
			if(hasActionErrors()) {
				return ActionResult.ERROR;
			} else {
				guiPatient = patientLogic.updatePatient(guiPatient);  // catch guiDoctor object details returned from updatePatient()
			}
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;
	}
	
	public String list(){
		if(isDoctorLogin() || isAssistantLogin()) {
			Index index = CommonUtil.getIndexBypage(page);
			if (StringUtils.isNotBlank(term) || sort != null) {
				guiPatientList = patientLogic.getPatientListByTerm(term, sort.getColumn(), index);
				guiPagination = patientLogic.getPaginationOfPatientsByTerm(term, sort.getColumn(), page);
				size = guiPagination.getTotalCount();
			} else {
				guiPatientList = patientLogic.getPatients(index);
				guiPagination = patientLogic.getPaginationOfPatients(page);
				size = guiPagination.getTotalCount();
			}		
			return ActionResult.SUCCESS;
		}
		return ActionResult.AUTH_FAILED;		
	}

	public PatientLogic getPatientLogic() {
		return patientLogic;
	}

	public void setPatientLogic(PatientLogic patientLogic) {
		this.patientLogic = patientLogic;
	}

	public GuiPatient getGuiPatient() {
		return guiPatient;
	}

	public void setGuiPatient(GuiPatient guiPatient) {
		this.guiPatient = guiPatient;
	}

	public List<GuiPatient> getGuiPatientList() {
		return guiPatientList;
	}

	public void setGuiPatientList(List<GuiPatient> guiPatientList) {
		this.guiPatientList = guiPatientList;
	}
	
	public boolean isExistGuiAppointment() {
		return guiAppointment != null && guiAppointment.isAvailable();
	}
	
	public GuiAppointment getGuiAppointment() {
		return guiAppointment;
	}

	public void setGuiAppointment(GuiAppointment guiAppointment) {
		this.guiAppointment = guiAppointment;
	}

	public String getTodayDate() {
		return todayDate;
	}

	public void setTodayDate(String todayDate) {
		this.todayDate = todayDate;
	}

	public boolean isTermExist() {
		return StringUtils.isNotBlank(term);
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}
	
	public boolean isSortExist() {
		return (sort != null);
	}

	public SortProperty getSort() {
		return sort;
	}
	
	public void setSort(String sortProperty) {
		this.sort = SortProperty.getValue(sortProperty);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public GuiPagination getGuiPagination() {
		return guiPagination;
	}

	public void setGuiPagination(GuiPagination guiPagination) {
		this.guiPagination = guiPagination;
	}
	
}
